package com.lily.aidouzixun.datacenter;

import android.content.Context;
import android.util.SparseArray;

import com.alibaba.fastjson.JSONObject;
import com.lily.aidouzixun.bean.UserInfo;
import com.lily.aidouzixun.bean.DynamicDetailHomeInfo;
import com.lily.aidouzixun.bean.DynamicHomeInfo;
import com.lily.aidouzixun.bean.ArticleHomeInfo;
import com.lily.aidouzixun.util.Validator;


/**
 * 数据提供类
 * 调用RemoteServer对应方法，解析json数据得到请求需要的对象
 */
public class DataProvider {
    private static final String TAG = "DataProvider";
    private static volatile DataProvider instance;
    private static final String UNKOWN_ERROR_CODE = "255";
    private static final String UNKOWN_ERROR_MESSAGE = "未知错误";
    private static final String NOT_NETWORK_CODE = "254";
    private static final String NOT_NETWORK_MESSAGE = "网络异常，请检查网络";

    /**
     * 用户json数据传递参数的key
     */
    /**
     * 状态码
     */
    private static final String ERRNO = "errno";
    /**
     * 消息提示
     */
    private static final String MESSAGE = "msg";
    /**
     * 数据
     */
    private static final String DATA = "data";
    private DataProvider() {

    }
    /**
     * 得带DataProvider对象
     * @return 这里为多线程单例
     */
    public static DataProvider getInstance() {
        if(instance == null) {
            synchronized (DataProvider.class) {
                if(instance == null) {
                    instance = new DataProvider();
                }
            }
        }
        return instance;
    }

    /**
     * 得到用户信息
     * @param appContext
     * @param urid
     * @return
     */
    public SparseArray<Object> getUserInfo(Context appContext, String urid) {
        SparseArray<Object> result = new SparseArray<Object>();
        String resultJson = RemoteServer.getUserInfo(appContext, urid);  //json数据
        if (!Validator.isEffective(resultJson)) {   //返回json数据为空
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject userJson =  infos.optJSONObject(this.DATA);
                    UserInfo userInfo = UserInfo.parseInfo(userJson);
                    if (userInfo != null) {
                        result.put(2, userInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }
    /**
     * 用户登录
     * @return 请求端需要的对象
     * String[]
     * result[0]-errno 255-未知错误 254-网络异常
     * result[1]-msg.
     * result[2]-userInfo.
     */
    public SparseArray<Object> userLogin(Context appContext, String userName, String userPwd) {
        SparseArray<Object> result = new SparseArray<Object>();
        String resultJson = RemoteServer.userLogin(appContext, userName, userPwd);  //json数据
        if (!Validator.isEffective(resultJson)) {   //返回json数据为空
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject userJson =  infos.optJSONObject(this.DATA);
                    UserInfo userInfo = UserInfo.parseInfo(userJson);
                    if (userInfo != null) {
                        result.put(2, userInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 用户注册
     * @param registerName
     * @param registerPwd
     * @return
     */
    public SparseArray userRegister(Context appContext, String registerName, String registerPwd) {
        String resultJson = RemoteServer.userRegister(appContext, registerName, registerPwd);  //json数据
        SparseArray<String> result = new SparseArray<String>();

        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject userJson =  infos.optJSONObject(this.DATA);
                    if (userJson.has("urid")) {
                        String urid = userJson.optString("urid");
                        if (Validator.isEffective(urid) && !("0".equals(urid))) {
                            result.put(2, urid);
                        }
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }

        return null;
    }

    /**
     * 修改用户信息
     * @param appContext
     * @param action
     * @param modifyContent
     * @return
     */
    public SparseArray modifyUserInfo(Context appContext, int action, String modifyContent) {
        String resultJson = RemoteServer.modifyUserInfo(appContext, action, modifyContent);  //json数据
        SparseArray<String> result = new SparseArray<String>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 修改密码
     * @param appContext
     * @param username
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public SparseArray modifyPassword(Context appContext, String username, String oldPwd, String newPwd) {
        String resultJson = RemoteServer.modifyPassword(appContext, username, oldPwd, newPwd);  //json数据
        SparseArray<String> result = new SparseArray<String>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 用户反馈
     * @param appContext
     * @param feedback
     * @return
     */
    public  SparseArray userFeedback(Context appContext, String feedback) {
        String resultJson = RemoteServer.userFeedback(appContext, feedback);  //json数据
        SparseArray<String> result = new SparseArray<String>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 文章首页
     * @param appContext
     *  @param page
     * @return
     */
    public  SparseArray<Object> getLongDistanceHome(Context appContext, int page) {
        String resultJson = RemoteServer.getLongDistanceHome(appContext, page);  //json数据;
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject jsonInfo =  infos.optJSONObject(this.DATA);
                    ArticleHomeInfo longDistanceHome = ArticleHomeInfo.parseInfo(jsonInfo);
                    if (longDistanceHome != null) {
                        result.put(2, longDistanceHome);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * todo 这里缓存没有比对时间
     * 得到动态信息
     * @param appContext
     * @return
     */
    public SparseArray<Object> getDiscussHome(Context appContext, int discussCategory, int pager) {
        String resultJson = RemoteServer.getDiscussHome(appContext, discussCategory, pager);  //json数据
        //把json数据放到数据库中
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) { //解析动态数据
                    JSONObject jsonInfo =  infos.optJSONObject(this.DATA);
                    DynamicHomeInfo discussHomeInfo = DynamicHomeInfo.parseInfo(jsonInfo);
                    if (discussHomeInfo != null) {
                        result.put(2, discussHomeInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 得到收藏的动态信息
     * @param appContext
     * @return
     */
    public SparseArray<Object> getDiscussCollect(Context appContext, int pager) {
        String resultJson = RemoteServer.getDiscussCollect(appContext, pager);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) { //解析动态数据
                    JSONObject jsonInfo =  infos.optJSONObject(this.DATA);
                    DynamicHomeInfo discussHomeInfo = DynamicHomeInfo.parseInfo(jsonInfo);
                    if (discussHomeInfo != null) {
                        result.put(2, discussHomeInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 发布动态文字
     * @param appContext
     * @param discussContent
     * @return
     */
    public SparseArray<Object> publishDiscussText(Context appContext, String discussContent,int hasImage) {
        String resultJson = RemoteServer.publishDiscussText(appContext, discussContent, hasImage);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject jsonInfo =  infos.optJSONObject(this.DATA);
                    if (jsonInfo.has("imagename")) {
                        result.put(2, jsonInfo.optString("imagename"));
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 发布动态照片
     * @param appContext
     * @param imageName
     * @param photoPath
     * @return
     */
    public SparseArray<Object> publishDiscussPhoto(Context appContext, String imageName, String photoPath) {
        String resultJson = RemoteServer.publishDiscussPhoto(appContext, imageName, photoPath);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (Validator.isEffective(resultJson) && resultJson .equals("200")) {    //成功
            result.put(0, "200");
            result.put(1, "上传成功！");
        } else {    //失败
            result.put(0, "255");
            result.put(1, "上传失败！");
        }
        return result;
        /*if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;*/
    }

    /**
     * 删除动态
     * @param context
     * @param did
     * @return
     */
    public SparseArray<Object> deleteDiscuss(Context context, String did) {
        String resultJson = RemoteServer.deleteDiscuss(context, did);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject jsonInfo =  infos.optJSONObject(this.DATA);
                    if (jsonInfo.has("imagename")) {
                        result.put(2, jsonInfo.optString("imagename"));
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 上传头像
     * @param appContext
     * @param avatarPath
     * @return
     */
    public SparseArray<Object> uploadAvatar(Context appContext, String avatarPath) {
        String resultJson = RemoteServer.uploadAvatar(appContext, avatarPath);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (Validator.isEffective(resultJson) && resultJson.equals("200")) {    //成功
            result.put(0, "200");
            result.put(1, "上传成功！");
        } else {    //失败
            result.put(0, "255");
            result.put(1, "上传失败！");
        }
        return result;
    }

    /**
     * 动态评论
     * @param appContext
     * @param did
     * @param comment
     * @return
     */
    public  SparseArray userDiscussComment(Context appContext, String did, String comment) {
        String resultJson = RemoteServer.userDiscussComment(appContext, did, comment);  //json数据
        SparseArray<String> result = new SparseArray<String>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 文章评论
     * @param appContext
     * @param ldid
     * @param comment
     * @return
     */
    public  SparseArray longDistanceComment(Context appContext, String ldid, String comment) {
        String resultJson = RemoteServer.longDistanceComment(appContext, ldid, comment);  //json数据
        SparseArray<String> result = new SparseArray<String>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 得到评论数据
     * @param appContext
     * @param did
     * @param did
     * @return
     */
    public  SparseArray commentData(Context appContext, String did, int commentPager) {
        String resultJson = RemoteServer.commentData(appContext, did, commentPager);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject dataJson = infos.getJSONObject(this.DATA);
                    DynamicDetailHomeInfo discussDetailHomeInfo = DynamicDetailHomeInfo.parseInfo(dataJson);
                    if (discussDetailHomeInfo != null) {
                        result.put(2, discussDetailHomeInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 点赞动态
     * @param appContext
     * @param did
     * @return
     */
    public  SparseArray supportDiscuss(Context appContext, String did, int supportAction) {
        String resultJson = RemoteServer.supportDiscuss(appContext, did, supportAction);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }
    /**
     * 收藏动态
     * @param appContext
     * @param did
     * @return
     */
    public  SparseArray collectDiscuss(Context appContext, String did, int action) {
        String resultJson = RemoteServer.collectDiscuss(appContext, did, action);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 反对评论
     * @param appContext
     * @param did
     * @return
     */
    public  SparseArray oppositionDiscuss(Context appContext, String did) {
        String resultJson = RemoteServer.oppositionDiscuss(appContext, did);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 绑定QQ
     * @param appContext
     * @param openid
     * @param nickName
     * @param avatar
     * @return
     */
    public  SparseArray bindQQ(Context appContext, String openid, String nickName, String avatar) {
        String resultJson = RemoteServer.bindQQ(appContext, openid, nickName, avatar);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject userJson =  infos.optJSONObject(this.DATA);
                    UserInfo userInfo = UserInfo.parseInfo(userJson);
                    if (userInfo != null) {
                        result.put(2, userInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 绑定手机号
     * @param appContext
     * @param phone
     * @return
     */
    public  SparseArray bindPhone(Context appContext, String phone) {
        String resultJson = RemoteServer.bindPhone(appContext, phone);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                if (infos.has(this.DATA)) {
                    JSONObject userJson =  infos.optJSONObject(this.DATA);
                    UserInfo userInfo = UserInfo.parseInfo(userJson);
                    if (userInfo != null) {
                        result.put(2, userInfo);
                    }
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

    /**
     * 用户签到
     * @param appContext
     * @return
     */
    public SparseArray userSign(Context appContext) {
        String resultJson = RemoteServer.userSign(appContext);  //json数据
        SparseArray<Object> result = new SparseArray<Object>();
        if (!Validator.isEffective(resultJson)) {   //
            result.put(0, this.NOT_NETWORK_CODE);
            result.put(1, this.NOT_NETWORK_MESSAGE);
            return result;
        }
        try {
            JSONObject infos = JSONObject.parseObject(resultJson);
            if (infos != null) {
                if (infos.has(this.ERRNO)) {
                    result.put(0, infos.optString(this.ERRNO));
                }
                if (infos.has(this.MESSAGE)) {
                    result.put(1, infos.optString(this.MESSAGE));
                }
                return result;
            }
        } catch (Exception e) {
            result.put(0, this.UNKOWN_ERROR_CODE);
            result.put(1, this.UNKOWN_ERROR_MESSAGE);
            return result;
        }
        return null;
    }

}
